<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>收款编辑</title>
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
	<script type="text/javascript">
	var basic_Url="${ctx}/logic/recept";
	var grid;
	var tax_price=null; 
	$(function(){
		var billState = '${recept.zd003Status}';
		var column1 = [
				      { display: 'id', name:'id', width: 1 ,hide:true},
					  { display: '收款单编号', name:'zd001ReptCode', width: 1,hide:true},
					  { display: '物料ID', name:'zd002MaterId',  width: 1 ,hide:true},
					  { display: '<font color="gray">产品编码</font>', name:'zd003MaterCode', width: 120},
					  { display: '<font color="gray">产品名称</font>', name:'zd004MaterName', width: 120},
	                  { display: '<font color="gray">单位</font>', name: 'zd012Unit', width: 120},
	                  { display: '<font color="gray">规格</font>', name: 'zd006Spec', width: 270},
	                  { display: '<font color="gray">数量</font>', name: 'zd020Quantity', width: 100},
	                  { display: '<font color="gray">单价(含税)</font>', name: 'zd013TaxPrice', width: 100}, 
	                  { display: '<font color="gray">总货款(含税)</font>', name: 'zd016Amount', width: 100,type: 'float',render: function(item){
	                	  if(item.zd016Amount == null || item.zd016Amount == "" || parseFloat(item.zd016Amount) == 0){
	                		  return "0.0";
	                	  }else{
	                		  return (item.zd016Amount*1).toFixed(4);
	                	  }
	                  }},
	                  { display: '<font color="gray">已收货款</font>', name: 'zd017HasMoney', width: 100,type: 'float',render:function(item){
	                	  if(item.zd017HasMoney == null || item.zd017HasMoney == "" || parseFloat(item.zd017HasMoney) == 0){
	                		  return "0.0";
	                	  }else{
	                		  return (item.zd017HasMoney*1).toFixed(4);
	                	  }
	          		  }},
	                  { display: '本次收款金额<font color="red">*</font>', name: 'zd018TheMoney', width: 100,type: 'float',editor: { type: 'float'},render: function(item){
	                	  if(item.zd018TheMoney == null || item.zd018TheMoney == "" || parseFloat(item.zd018TheMoney) == 0){
	                		  return "0.0";
	                	  }else{
	                		  return (item.zd018TheMoney*1).toFixed(4);
	                	  }
	                  }},
	                  { display: '<font color="gray">未收货款</font>', name: 'zd019NoMoney', width: 100,type: 'float',render: function(item){
	                	  if(item.zd019NoMoney == null || item.zd019NoMoney == "" || parseFloat(item.zd019NoMoney) == 0){
	                		  return "0.0";
	                	  }else{
	                		  return (item.zd019NoMoney*1).toFixed(4);
	                	  }
	                  }}
	                  ];
		var column2 = [{ display: 'id', name:'id', width: 1 ,hide:true},
					  { display: '收款单编号', name:'zd001ReptCode', width: 1,hide:true},
					  { display: '物料ID', name:'zd002MaterId',  width: 1 ,hide:true},
					  { display: '<font color="gray">产品编码</font>', name:'zd003MaterCode', width: 120},
					  { display: '<font color="gray">产品名称</font>', name:'zd004MaterName', width: 120},
	                  { display: '<font color="gray">单位</font>', name: 'zd012Unit', width: 120},
	                  { display: '<font color="gray">规格</font>', name: 'zd006Spec', width: 270},
	                  { display: '<font color="gray">数量</font>', name: 'zd020Quantity', width: 100},
	                  { display: '<font color="gray">单价(含税)</font>', name: 'zd013TaxPrice', width: 100}, 
	                  { display: '<font color="gray">总货款(含税)</font>', name: 'zd016Amount', width: 100},
	                  { display: '<font color="gray">已收货款</font>', name: 'zd017HasMoney', width: 100},
	                  { display: '本次收款金额<font color="red">*</font>', name: 'zd018TheMoney', width: 100},
	                  { display: '<font color="gray">未收货款</font>', name: 'zd019NoMoney', width: 100}
	                  ];
		var column3 = null;
		column3 = column1;
		grid = $("#table1").ligerGrid({
	        columns: column3,
	        enabledEdit: true,
	        usePager: true,pageSize:1000,pageSizeOptions:[1000],
	        parms:[{ name : 'receptId' , value : '${recept.id}'}],
	      	url : basic_Url+"/gridItems.do",
	        width: "100%", height:235, checkbox: true,
	        toolbar: { items: [
	                           { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
	                           ]
	                           }
	    });
		$("#pageloading").hide();

		$("#inputForm").validate({			   
			onsubmit: true,
			 /*执行ajaxsubmit  */ 
		    submitHandler: function(editform) {
		        try{			    		
		            url =  basic_Url+"/save.do";
		            var api = frameElement.api;
		        	var W = api.opener;
		            getReturnMessage(url,"inputForm",W.searchAP);
		            W.pid='${pid}';
		        }catch(e){alert(e);}
		    }
		});
		<%--
		//销售出库单
		$("#zd002SaleId").change(function(){ 
			var prodoutId = $("#zd002SaleId").val();
			$("#zd002SaleCode").val($("#zd002SaleId").find("option:selected").text());
			$.ajax({
				url: basic_Url+"/prodSaleQuery.do",
		  	      global: false,
		  	      type: "POST",
		  	      dataType: "json",
		  	      data: ({
		  	    	prodoutId : prodoutId,
		  	    	saleCode : $("#zd002SaleId").find("option:selected").attr("cvalue")
		  	      }),		     
		  	      success: function(msg){
		  	    	  alert(msg.zd009CustName);
		  	    	  $("#zd009CustId").val(msg.zd009CustId);
		  	    	  $("#zd009CustCode").val(msg.zd009CustCode);
		  	    	  $("#zd009CustName").val(msg.zd009CustName);
		  	    	  
		  	    	  $("#zd010ExRateCode").val(msg.zd010ExRateCode);
		  	    	  $("#zd010ExRateName").val(msg.zd010ExRateName);
		  	    	  $("#zd010ExRateVal").val(msg.zd010ExRateVal);
		  	    	  
		  	    	  $("#zd004SumAmount").val(msg.zd004SumAmount);
		  	    	  $("#zd005HasAmount").val(msg.zd005HasAmount);
		  	      }
			});
			var path = basic_Url+"/prodSaleItemList.do?prodoutId="+prodoutId;
			multipleSearch("#table1",true,"",encodeURI(path));
		}); 
		--%>
		//付款方式
		$("#zd008PayModeId").change(function(){
			$("#zd008PayModeCode").val($("#zd008PayModeId").find("option:selected").attr("cvalue"));
			$("#zd008PayModeName").val($("#zd008PayModeId").find("option:selected").text());
		});
		//按钮显示
		if("${flag}" == "ADD"){ //新增单据
			$("#commit").hide();//禁用提交按钮
			$("#skExportExcel").hide();
		}
		if('REP_SAVE'==billState){
			$("#save").hide();//禁用保存按钮
			$("#skExportExcel").hide();
		}
		if('REP_SUBMIT'==billState){
			$("#save").hide();//禁用保存按钮
			$("#commit").hide();//禁用提交按钮
		}
	});

function submitForm(param){
    //设置ajax全局同步
 	
	var data = grid.getData();
	var dl=grid.getDeleted();
	if(data.length==0){
		$.dialog.alert("请选择收款内容");
   	 	return false;
	}
	var ba=true;
	if(data !=null){
		var i=0;
		var sumsshk=0;
		$(data).each(function (){
			i++;
			sumsshk += parseFloat(this.zd018TheMoney);
			if(this.zd017HasMoney == null || this.zd017HasMoney.length<=0){
				this.zd017HasMoney = 0;
			}
			if((parseFloat(this.zd018TheMoney))>parseFloat(this.zd019NoMoney)){
				$.dialog.alert("您第" + i + "行的本次收款金额大于未收款金额");
				ba = false;
				return false;
			} 
			if(this.zd018TheMoney<=0){
				$.dialog.alert("您第" + i + "行的收款金额必须大于0");
				ba = false;
				return false;
			}
		});
		if(sumsshk<=0){
			$.dialog.alert("您的本次收款金额不合法，请重新填写");
			ba = false;
			return false;
		}
	}
	jQuery.ajaxSettings.async = false;
	if(ba){
		$("#dataitem").val(JSON.stringify(data));
		$("#datadele").val(JSON.stringify(dl));
	  	$("#flagMark").val(param);
	    $("#inputForm").submit();
	}
 }		
 
function tableSubmit(msg,param){
	$.ligerDialog.confirm(msg, function(flag){ 
		if(flag){
			submitForm(param);
		}
	}); 
}

function submitPay(param){
	tableSubmit('是否确认提交收款？',param);
}
	
function deleteRow(){ 
	grid.deleteSelectedRow();
}
function exportAP(){
	var id=$("#id").val();
	if(id!=""){
		var path = basic_Url+"/exportExcel.do?id="+id;
   		window.open(path,"_blank");
	}
	else{
		$.dialog.alert("请先保存再导出");
	}
} 



//销售出库
function  customerWindow() {
	getDG().dialog(
			{
				title : '选择出库单号',
				width : 650,
				height : 350,
				content : 'url:' + sys_context + '/public/windowalert/recept.do',    
				lock : true,
				max : true,
				min : true,
				ok : function() {
					var rows = this.content.getSelected();
					if(rows==null){
						alert("请选择数据");
						return;
					}
					else{
						if(rows.length>1){
							alert('只能选择一行数据!');
							return;
						}else{
							$(rows).each(function() {
						     	$("#zd002SaleId").val(this.id);
								$("#zd002SaleCode").val(this.zd001OutCode);
								$("#zd013CurrencyName").val(this.zd010ExRateName);
								$("#zd013CurrencyCode").val(this.zd010ExRateCode);
								$("#zd009CustName").val(this.zd012CustomerName);
					    		$("#zd009CustId").val(this.zd012CustomerId);
								$("#zd009CustCode").val(this.zd012CustomerCode);
								$.ajax({
									url: basic_Url+"/prodSaleQuery.do",
							  	      global: false,
							  	      type: "POST",
							  	      dataType: "json",
							  	      data: ({
							  	    	prodoutId :this.id,
							  	    	saleCode : this.zd001OutCode
							  	      }),		     
							  	      success: function(msg){
							  	    	  $("#zd010ExRateCode").val(msg.zd010ExRateCode);
							  	    	  $("#zd010ExRateName").val(msg.zd010ExRateName);
							  	    	  $("#zd010ExRateVal").val(msg.zd010ExRateVal);
							  	    	  $("#zd004SumAmount").val(msg.zd004SumAmount);
							  	    	  $("#zd005HasAmount").val(msg.zd005HasAmount);
							  	    	var path = basic_Url+"/prodSaleItemList.do?prodoutId="+	$("#zd002SaleId").val();
										multipleSearch("#table1",true,"",encodeURI(path));
							  	      }
								});
							
						});
						}
					}
					
					this.content.Close();
					return false;
				},
				cancelVal : '关闭',
				cancel : true
			}).zindex();
	
}
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${recept.id}"/>
<input type="hidden" id="bizVersion" name="bizVersion" value="${recept.bizVersion}"/>
<input type="hidden" id="dataitem" name="dataitem" value=""/>
<input type="hidden" id="datadele" name="datadele" value=""/>
<!-- 用于标识提交按钮 -->
<input type="hidden" id="flagMark" name="flagMark" value="" />
 <div class="pageContent" >
    <div class="panel">
    <table class="TableBlock" align="center">
    	<c:if test="${!empty recept.zd001ReptCode}">
    	<tr>
	        <td nowrap="nowrap">收款单号:</td>
	        <td >
	        	<input type="text" id="zd001ReptCode" name="zd001ReptCode" class="inp_width_98p" readonly="readonly"  value="${recept.zd001ReptCode}" />
	        </td>
	        <td nowrap="nowrap" width="20%">建单时间:</td>
			<td width="30%">
				<input class="inp_width_98p" type="text" id="bizCreatorDate" name="bizCreatorDate"
				value="<fmt:formatDate value="${empty recept.bizCreatorDate?nowDate:recept.bizCreatorDate}" type="both" dateStyle="long" pattern="yyyy-MM-dd" />" readonly="readonly"/>
			</td>
  		</tr>
  		</c:if>
	    <tr>
    		<td nowrap="nowrap">销售出库单号:</td>
    			<c:choose>
    			<c:when test="${empty recept.zd002SaleId}">
	    			<td>
		    			<!-- 
		    			<select id="zd002SaleId" name="zd002SaleId" class="inp_width_98p required" >
		                 	<option></option>
		                 	<c:forEach items='${prodOutList}' var='prodout'>
		                 		<option value="${prodout.id }" cvalue="${prodout.zd002OriCode }">${prodout.zd001OutCode }</option>
		                 	</c:forEach>
		                </select> 
		                 -->
            		   	<input type="hidden" id="zd002SaleId" name="zd002SaleId" />
	                	<input type="text" id="zd002SaleCode" name="zd002SaleCode"   />	  
	                	<input type="button" value="..." onclick="customerWindow();" />              	
	            	</td>
            	</c:when>
            	<c:otherwise>
            		<td>
            			<input type="hidden" id="zd002SaleId" name="zd002SaleId" value="${recept.zd002SaleId}" class="inp_width_98p" readonly="readonly"/>
            			<input type="text" id="zd002SaleCode" name="zd002SaleCode" value="${recept.zd002SaleCode}" class="inp_width_98p" readonly="readonly"/>
            		</td>
           		</c:otherwise>
            	</c:choose>  
            <td nowrap="nowrap">收款日期:</td>
            <c:choose>
	            <c:when test="${empty recept.zd014ReptDate}">
	            	<td>
	            		<input type="text" id="zd014ReptDate" name="zd014ReptDate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="inp_width_98p Wdate required" value="<fmt:formatDate value="${recept.zd014ReptDate}" pattern="yyyy-MM-dd" type="both" />" />
	            	</td>
	        	</c:when>
	        	<c:otherwise>
	        		<td>
	        			<input type="text" id="zd014ReptDate" name="zd014ReptDate"  class="inp_width_98p Wdate" value="<fmt:formatDate value="${recept.zd014ReptDate}" pattern="yyyy-MM-dd" type="both" />"  readonly="readonly" />
	        		</td>
	        	</c:otherwise>
        	</c:choose> 
   		 </tr>
         <tr>
	     	<td nowrap="nowrap">客户名称：</td>
	     	<td>
	     		<input type="text" id="zd009CustName" name="zd009CustName" value="${recept.zd009CustName}" class="inp_width_98p" readonly="readonly" />
	     	</td>
         	<td nowrap="nowrap">客户编码:</td>
         	<td>
         		<input type="text" id="zd009CustCode" name="zd009CustCode" value="${recept.zd009CustCode}" class="inp_width_98p" readonly="readonly"/>
   		    	<input type="hidden" id="zd009CustId" name="zd009CustId"  value="${recept.zd009CustId}" />
   		    </td>
	     </tr>
	     <tr>
	     	<td nowrap="nowrap">付款方式:</td>
	     	<td>
	     		<c:choose>
	     			<c:when test="${recept.zd008PayModeId==null or recept.zd008PayModeId =='' }">
	     				<select id="zd008PayModeId" name="zd008PayModeId" class="inp_width_98p required">
			     			<option></option>
			     			<c:forEach var="pay" items="${payList }">
			     				<option value="${pay.id }" cvalue="${pay.zd001Code }">${pay.zd002Name }</option>
			     			</c:forEach>
	     				</select>
	     					<input type="hidden" id="zd008PayModeCode" name="zd008PayModeCode" value="${recept.zd008PayModeCode }"/>
	     					<input type="hidden" id="zd008PayModeName" name="zd008PayModeName" value="${recept.zd008PayModeName }"/>
	     			</c:when>
	     			<c:otherwise>
	     				<input type="hidden" id="zd008PayModeId" name="zd008PayModeId" value="${recept.zd008PayModeId }"/>
	     				<input type="hidden" id="zd008PayModeCode" name="zd008PayModeCode" value="${recept.zd008PayModeCode }"/>
	     				<input type="text" id="zd008PayModeName" name="zd008PayModeName" value="${recept.zd008PayModeName }" class="inp_width_98p" readonly="readonly"/>
	     			</c:otherwise>
	     		</c:choose>
		     </td>
	     	<td nowrap="nowrap">币别：</td>
            <td>
          
            	<input type="hidden" id="zd010ExRateCode" name="zd010ExRateCode" value=""/>
            	<input type="text" id="zd010ExRateName" name="zd010ExRateName" value="${recept.zd010ExRateName}" class="inp_width_98p" readonly="readonly"></input>
		     </td>     	 	                    
	     </tr>
	     <tr>  		                 
         	<td nowrap="nowrap">付款账号：</td>         	
            <td>
	           <input type="text" name="zd007PayAccount" id="zd007PayAccount"  value="${recept.zd007PayAccount}" class="inp_width_98p required" />
            </td>
	     	<td nowrap="nowrap">汇率:</td>
	     	<td>
	     		<input type="text" id="zd010ExRateVal" name="zd010ExRateVal" value="${recept.zd010ExRateVal}" class="inp_width_98p" readonly="readonly" />
	     	</td>
	     </tr>
         <tr>
	        <td nowrap="nowrap">应收总货款:</td>
	        <td >
	        	<input type="text" id="zd004SumAmount" name="zd004SumAmount" class="inp_width_98p" readonly="readonly" value="${recept.zd004SumAmount}" />
	        </td>
	        <td nowrap="nowrap">已收货款:</td>
	        <td >
	        	<input type="text" id="zd005HasAmount" name="zd005HasAmount" class="inp_width_98p" readonly="readonly"  value="${recept.zd005HasAmount}" />
	        	<c:if test="${recept.zd005HasAmount == null}">
					<script>
						$("#yshk").val("0");
					</script>
				</c:if>
	        </td>
  		</tr>
		<tr>
			<td nowrap="nowrap">制单人:</td>
			<td >
			    <input type="text" id="bizCreatorName" name="bizCreatorName" value="${user.name}" class="inp_width_98p" readonly="readonly"/>
				<c:if test="${recept.bizCreatorName != null}">
					<script>
						$("#bizCreatorName").val("${recept.bizCreatorName}");
					</script>
				</c:if>
		  </td>
		</tr>
        <tr>
         	<td nowrap="nowrap">备注:</td>
         	<td colspan="3">
         		<input type="text" id="zd013Remark" name="zd013Remark" class="inp_width_98p" value="${recept.zd013Remark}" />
         	</td>
        </tr>
     </table>
     <div id="table1"> </div>               
     </div>
    <div class="formBar">
	        <ul>
	           <li><div class="button"><div class="buttonContent"><button type="button" id="skExportExcel" class="export" onclick="exportAP()">导出</button></div></div> </li>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm('REP_SAVE')">保存草稿</button></div></div></li>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="commit" onclick="submitPay('REP_SUBMIT')">提交收款</button></div></div></li>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
	        </ul>
        </div>    
    </div>
</form>
</body>
</html>
